﻿#include "FileFunction.h"

FileFunction::FileFunction(QObject *parent) : QObject(parent)
{

}

bool FileFunction::getDirList(QString filePath, QList<QFileInfo> *l_fileList)
{
    QDir dir(filePath);

    if (!dir.exists())
    {
        return false;
    }

    dir.setFilter(QDir::Dirs|QDir::Files);
    dir.setSorting(QDir::DirsFirst);
    QFileInfoList list = dir.entryInfoList();
    int i = 0;
    bool bIsDir;
    do{
        QFileInfo fileInfo = list.at(i);
        if( (fileInfo.fileName()==".") | (fileInfo.fileName()=="..") )
        {
            i++;
            continue;
        }
        bIsDir = fileInfo.isDir();

        if (bIsDir)
        {
            l_fileList->append(fileInfo);
        }
        i++;
    }while(i<list.size());
    return true;
}

bool FileFunction::getAllFileInfoList(QString filePath , QList <QFileInfo>* l_fileList)
{
    QFileInfo fileInfo(filePath);
    if(fileInfo.isFile())
    {
        l_fileList->append(fileInfo);
        return true;
    }

    QDir dir(filePath);

    if (!dir.exists())
    {

        return false;
    }

    dir.setFilter(QDir::Dirs|QDir::Files);
    dir.setSorting(QDir::DirsFirst);
    QFileInfoList list = dir.entryInfoList();
    int i = 0;

    do{
        fileInfo = list.at(i);
        if( (fileInfo.fileName()==".") | (fileInfo.fileName()=="..") )
        {
            i++;
            continue;
        }

        if (fileInfo.isDir())
        {
            QString str = fileInfo.filePath();
            getAllFileInfoList(str,l_fileList);
        }else if(fileInfo.isFile()){
            l_fileList->append(fileInfo);
        }
        i++;
    }while(i<list.size());

    return true;
}

bool FileFunction::isExitFile(QString fileName, QString path)
{
    QFile file(path+fileName);
    if(file.exists())
    {
        return true;
    }
    return false;
}

bool FileFunction::isDirExistAndMake(QString path)
{
    QDir dir(path);
    if(dir.exists())
    {
        return true;
    }
    else
    {
        QDir dirMake("./");
        dirMake.mkpath(path);
        return true;
    }
    return false;
}

bool FileFunction::DelDir(const QString &path)
{
    if (path.isEmpty()){
        return false;
    }
    QDir dir(path);
    if(!dir.exists()){
        return true;
    }
    dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); 
    QFileInfoList fileList = dir.entryInfoList(); 
    foreach (QFileInfo file, fileList){ 
        if (file.isFile()){ 
            file.dir().remove(file.fileName());
        }else{ 
            DelDir(file.absoluteFilePath());
        }
    }
    return dir.rmpath(dir.absolutePath()); 

}

bool FileFunction::copyFile(QString srcPath, QString dstPath)
{
    QFileInfo info;
    info.setFile(srcPath);
    if(info.exists()){
        if(!QFile::copy(srcPath, dstPath.append(info.fileName()))){
            return false;
        }
    }else{
        return false;
    }
    return true;
}

bool FileFunction::readSmallFile(QString Path, QByteArray &array)
{
    QFile   aFile(Path);
    if (!aFile.exists()) //文件不存在
        return false;
    if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;
    array = aFile.readAll();
    aFile.close();
}

